<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2021/8/16
 * Time: 14:55
 */

namespace app\command;


use app\service\AliyunService;
use app\service\SkuService;
use GuzzleHttp\Psr7\Request;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Writer\Csv;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use QL\QueryList;
use QL\Ext\Chrome;
use think\Exception;
use think\facade\Db;


class Lazada  extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('lazada service')
            ->setDescription('自动爬取www.lazada.co.id');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->curl();
        echo '遍历任务'.date('Y-m-d H:i:s').PHP_EOL;

        /*$startUrl = 'https://www.lazada.co.id/beli-handphone/xiaomi/?spm=a2o4j.searchlistcategory.card.2.763612d2lfK3qy&&from=onesearch_category_3443';
        $urlArr = $this->getAllUrl($startUrl);
       if(empty($urlArr)){
           echo '没有获取到网址';die;
       }
       foreach ($urlArr as $url){
           if(is_array($url)){
               foreach ($url as $u){
                   echo '正在采集：'.$u.PHP_EOL;
                   $this->saveData($u);
               }
           }else{
               echo '正在采集：'.$url.PHP_EOL;
               $this->saveData($url);
           }

       }*/
     }

     public function curl(){
         $url =  Db::name('lkt_lazada')->where(['status'=>0])->order('id asc')->find();
         $url['url'] = 'https://www.lazada.co.id/products/realme-8-realme-8-pro-original-8gb-ram-128gb-rom-garansi-resmi-nfc-original-handphone-realme-hp-realme-8pro-new-2021-android-11-murah-promo-bisa-cod-cicilan-kredit-free-shipping-gratis-ongkir-i953784873-s9422842132.html?spm=a2o4j.searchlistcategory.list.52.45b212d2f6qUN6&search=1&freeshipping=1';
         # $url['url'] = 'https://www.lazada.co.id/products/han-river-food-procesor-2l-blender-daging-serbaguna-stainless-steel-food-chopper-serbaguna-meat-grinder-listrik-i1245610137-s5062126964.html?spm=a2o4j.home.flashSale.2.69951559iEhscQ&search=1&mp=1&c=fs&clickTrackInfo=rs%3A0.0%3Babid%3A238030%3Bitem_id%3A1245610137%3Bpvid%3A2e2e746d-22eb-4417-9816-b1c58aa1fbb0%3Bmt%3Ai2i%3Bdata_type%3Aflashsale%3Bscm%3A1007.17760.238030.%3Bchannel_id%3A0000%3Bcampaign_id%3A135610&scm=1007.17760.238030.0';
         $url['id'] =1;
         $url['product_class'] = '-753-759-';
         $url['brand_class'] = '228';
         if(empty($url)){
             return true;
         }
         echo '正在采集'.$url['url'].PHP_EOL;
         #列表
         if(strstr($url['url'], "www.lazada.co.id/products/")==false){
             $update = array(
                 'status'=>1
             );
             Db::name('lkt_lazada')->where(['id'=>$url['id']])->update($update);
         }else{
             $update = array(
                 'status'=>1
             );
             Db::name('lkt_lazada')->where(['id'=>$url['id']])->update($update);
           return   $this->saveData($url['url'],$url['product_class'],$url['brand_class']);
         }
         return false;
     }

     public function getHtml($url){
        try{
            $client = new \GuzzleHttp\Client();
            $res = $client->request('GET', $url, [
                'headers' => [
                    'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
                    'Accept-Encoding' => 'gzip, deflate, br',
                ],
                'timeout' =>30
            ]);
        }catch (\Throwable $e){
            $time = mt_rand(10,30);
            echo '采集页面超时，等待'.$time.'从新开启'.$url.PHP_EOL;
            sleep($time);
           return  $this->getHtml($url);
        }
         echo '采集页面成功'.$url.PHP_EOL;
       return (string)$res->getBody();
     }
    /**
     * @param $url
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function saveData($url,$product_class,$brand_id){
        $store_id = 113;
        $mch_id = 226;
        $s_type = '';
        $active = 1;#活动类型
        $show_adr = '';
        if(strpos($url,'www.lazada.co.id')===false){
            $url = 'https://www.lazada.co.id'.$url;
        }
        if(strpos($url,'http')===false){
            $url = 'http:'.$url;
        }

        $html = $this->getHtml($url);
      #  file_put_contents('index.html',   $html);
        $ql = QueryList::html($html);
        $data['title'] = $ql->find('h1.pdp-mod-product-badge-title')->text();
        $data['price'] = $ql->find('div.pdp-product-price .pdp-price_type_normal')->text();
        $data['price'] = trim($data['price'],'Rp');
        $data['price'] = str_replace('.','',$data['price']);
        $data['priceDiscount'] = $ql->find('div.pdp-product-price .pdp-price_type_deleted')->text();
        $data['discount'] = $ql->find('div.pdp-product-price .pdp-product-price__discount')->text();
        $data['scan'] = '';
        $imgurls  = $ql->find('div  .item-gallery__image-wrapper img')->attrs('src')->all();
        $data['imglist_str'] = '';
        $unit = '台';

        if(!empty( $imgurls)){

            foreach ( $imgurls as $k=>$val){
                if(strpos($val,'"https:') !== false){
                    unset($imgurls[$k]);
                    continue;
                }
                $val = str_replace('_120x120q80.jpg_.webp','',$val);
                $val = 'http:'.$val;
                #阿里云转存储
                $val = AliyunService::uploadByUrl( $val,$store_id);
                if(empty($data['imglist_str'])){
                    $data['imglist_str'] = $val;
                }else{
                    $data['imglist_str'] = $data['imglist_str'].','.$val;
                }
                $imgurls[$k] = $val;
            }
        }
        if(strpos($imgurls[0],'/p/')===false){
            $cover = $imgurls[1];
            }else{
            $cover = $imgurls[0];
        }

       # $imgurls =  array_slice($imgurls,1,4);
        $data['imglist'] = array_slice($imgurls,0);
        preg_match('/"skuBase":{"properties":(.*),"skus/', $html, $pat_array);
        $sku = json_decode($pat_array[1],true);
        preg_match('/"highlights":"(.*?)"/', $html, $highlights_array);
        preg_match('/"desc":(.*?)"highlights"/',$html, $desc_array);
        preg_match('/"features":(.*?)"tracking"/', $html, $features_array);
        preg_match('/"pdt_name":"(.*?),"page/', $html, $name);
        $content = '';
        if(!empty($name[1])){
            $content = "Spesifikasi dari ".trim($name[1],'"');
        }
        if(!empty($highlights_array[0])){
            $content =  $content.$highlights_array[0];
        }

     if(!empty($desc_array[0])){
         $content = $content.$desc_array[0];
     }
        $content = str_replace(['&bull;','desc:','"highlights":"','highlight','"','""desc":"'],'',$content);
        $content = str_replace('desc:','', $content);
        $content = str_replace(",s",'', $content);
        $content = stripslashes($content);
        $data['content']=$content;
        $cover_map = $cover;#封面图
        Db::startTrans();
        try{
            /*  插入产品lkt_product_list
                插入产品轮播图lkt_product_img
                插入产品每个sku属性lkt_configure
                库存表增加总库存lkt_stock
                修改商品编码lkt_product_number*/
            $kucun = 10000;
            if(!empty($sku)){
                $skuArr = array();
                foreach ($sku as $k=>$val){
                    if(empty($val['values'])){
                        continue;
                    }
                    if($k==0){
                        foreach ($val['values'] as $values){
                            $s = array();
                            $s['cbj'] = $data['price'];
                            $s['yj'] = $data['price'];
                            $s['sj'] = $data['price'];
                            $s['unit'] = $unit;
                            $s['kucun'] = $kucun;
                            $s['image'] = empty($values['image'])?$cover:$values['image'];
                            $s['bar_code'] ='';
                            if(!isset($s['attr_list'])){
                                $s['attr_list'] = array();
                            }
                            $s['attr_list'][] = [
                                'attr_id'=>'',
                                'attr_name'=>$values['name'],
                                'attr_group_name'=>$val['name'],
                            ];
                            if(strpos($s['image'],'http')===false){
                                $s['image'] = 'http:'.$s['image'];
                            }
                            $s['image'] = AliyunService::uploadByUrl($s['image'],$store_id);
                            $s['img'] = $s['image'];
                            $skuArr[] = $s;
                        }
                    }else{
                        $newSkuArr = array();
                        foreach ($skuArr as $k=>$skuData){
                            $temp =  $skuData;
                            foreach ($val['values'] as $values){
                                $temp['attr_list'][] = [
                                    'attr_id'=>'',
                                    'attr_name'=>$values['name'],
                                    'attr_group_name'=>$val['name'],
                                ];
                                $newSkuArr[] = $temp;
                                $temp = $skuData;
                            }
                        }
                        $skuArr = $newSkuArr;
                    }
                }
            }else{
                $s['cbj'] = $data['price'];
                $s['yj'] = $data['price'];
                $s['sj'] = $data['price'];
                $s['unit'] = $unit;
                $s['kucun'] = $kucun;
                $s['image'] = $cover;
                $s['bar_code'] ='';
                $s['attr_list'][] = [
                    'attr_id'=>'',
                    'attr_name'=>'Bawaan',
                    'attr_group_name'=>'Bawaan',
                ];
               # AliyunService::uploadByUrl($s['image'],$store_id);
                $s['img'] = $s['image'];
                $skuArr[] = $s;
            }
            $attr =  $skuArr;

            $totalKucun = $kucun*(count($skuArr));
            $initial['cbj'] = $data['price'];
            $initial['yj'] = $data['price'];
            $initial['sj'] = $data['price'];
            $initial['unit'] = $unit;
            $initial['kucun'] = $totalKucun;
            $admin_name = '系统';

            #校验属性
          /*  $where = array(
                'store_id'=>$store_id,
                'recycle'=>0,
                'product_title'=>$data['title'],
                'mch_id'=>$mch_id,
            );
            $goodData = Db::name('lkt_product_list')->where($where)->find();
            #商品存在，跳过
            if(!empty( $goodData)){
                echo '存在商品数据'.PHP_EOL;
                Db::rollback();
            }*/
            foreach ($initial as $k => $v)
            {
                if ($k == 'cbj' && $v == '')
                {
                    echo ' 成本价初始值不能为空';
                    Db::rollback();
                }
                else if ($k == 'yj' && $v == '')
                {
                    echo ' 原价初始值不能为空';
                    Db::rollback();
                }
                else if ($k == 'sj' && $v == '')
                {
                    echo  ' 售价初始值不能为空';
                    Db::rollback();
                }
                else if ($k == 'unit' && $v == '0')
                {
                    echo ' 单位初始值不能为空';
                    Db::rollback();
                }

                if ($k == 'cbj' || $k == 'yj' || $k == 'sj')
                {
                    if (is_numeric($v))
                    {
                        if ($v < 0)
                        {
                            echo ' 初始值不能为负数';
                            Db::rollback();
                        }
                    }
                    else
                    {
                        echo ' 初始值请填写数字';
                        Db::rollback();
                    }
                }
            }
            $initial = serialize($initial);
            $foreach_num = 0;
            $type = 'Pc';
            $z_num = 0;
            $attributes = array();
            $type0 = '平台';
            $min_inventory = 10;
            $isVirtual = 0;
            $volume = 100;
            foreach ($attr as $k => $v)
            {
                $foreach_num++;
                foreach ($v['attr_list'] as $ke => $va)
                {
                    $attr_group_name_id =  SkuService::getId( $store_id, 1, $va['attr_group_name'], 0);
                    if ($attr_group_name_id != 0)
                    { // 当属性名称ID不为0，SKU表里有数据
                        $attr_name_id =SkuService::getId( $store_id, 2, $va['attr_name'], $attr_group_name_id);
                        if ($attr_name_id == 0)
                        { // 当属性值ID为0，SKU表里没有数据
                            #$attr_name_id = $Tools->add_attribute($db, $store_id, $attr_group_name_id, $va['attr_name'], 2, $admin_name, $foreach_num,$type); // 添加属性值
                            $attr_name_id = SkuService::addSku( $store_id, $attr_group_name_id, $va['attr_name'],  $admin_name, $foreach_num,2); // 添加属性值
                        }
                    }
                    else
                    { // 当属性名称ID为0，SKU表里没有数据
                        $attr_group_name_id = SkuService::addSku($store_id, 0, $va['attr_group_name'],  $admin_name, 0,1); // 添加属性名
                        $attr_name_id = SkuService::addSku( $store_id, $attr_group_name_id, $va['attr_name'], $admin_name, $foreach_num,2);
                    }
                    $attr[$k]['attr_list'][$ke]['attr_group_name'] = $va['attr_group_name'] . '_LKT_' . $attr_group_name_id; // 拼接属性名
                    $attr[$k]['attr_list'][$ke]['attr_name'] = $va['attr_name'] . '_LKT_' . $attr_name_id; // 拼接属性值
                }
                foreach ($attr as $key => $value)
                {
                    $attr_list = $value['attr_list'];
                    $attr_list_arr = array();
                    $attr_list_srt = '';
                    foreach ($attr_list as $k => $v)
                    {
                        $attr_list_arr[$v['attr_group_name']] = $v['attr_name'];
                        $attr_list_srt .= substr($v['attr_group_name'], 0, strpos($v['attr_group_name'], '_')) . '-' . substr($v['attr_name'], 0, strpos($v['attr_name'], '_'));
                    }

                    $z_num += $value['kucun'];
                    $value['total_num'] = $value['kucun'];
                    if($type0 == '平台')
                    {
                        if(isset($value['img']))
                        {
                            if ($value['img'] == '')
                            {
                                echo  ' 的属性图片未上传';
                                Db::rollback();
                            }
                        }
                        else
                        {
                            echo   $attr_list_srt . ' 的属性图片未上传';
                            Db::rollback();
                        }
                        $attributes[$key]['img'] = preg_replace('/.*\//', '', $value['img']);
                        $attributes[$key]['unit'] = $value['unit'];
                    }
                    else if($type0 == 'PC店铺')
                    {
                        if ($value['image'] == '')
                        {
                            echo 'Attribute_image_of_was_not_uploaded';
                            Db::rollback();
                        }
                        $attributes[$key]['img'] = preg_replace('/.*\//', '', $value['image']);
                        $attributes[$key]['unit'] = $value['unit'];
                    }
                    else if($type0 == '店铺')
                    {
                        if(isset($value['img']))
                        {
                            if ($value['img'] == '')
                            {
                                echo   $attr_list_srt . ' 的属性图片未上传';
                                Db::rollback();
                            }
                        }
                        else
                        {
                            echo   $attr_list_srt . ' 的属性图片未上传';
                            Db::rollback();
                        }
                        $attributes[$key]['img'] = preg_replace('/.*\//', '', $value['img']);
                        $attributes[$key]['unit'] = $unit;
                    }
                    //价格判断
                    foreach ($value as $cvkey => $cvvalue)
                    {
                        if (!is_array($cvvalue))
                        {
                            if (empty($cvvalue) && $cvvalue != 0)
                            {
                                echo ' 请完善属性';
                                Db::rollback();
                            }
                        }
                    }

                    if(!is_numeric($value['cbj'])){
                        echo  $attr_list_srt . ' 的成本价不为数字！';
                        Db::rollback();
                    }
                    if(!is_numeric($value['yj'])){
                        echo  $attr_list_srt . ' 的原价不为数字！';
                        Db::rollback();
                    }
                    if(!is_numeric($value['sj'])){
                        echo  $attr_list_srt . ' 的售价不为数字！';
                        exit();
                    }
                    if(!is_numeric($value['kucun'])){
                        echo  $attr_list_srt . ' 的库存不为数字！';
                        Db::rollback();
                    }
                    $costprice = $value['cbj'];
                    $price = $value['sj'];
                    if ($costprice > $price)
                    {
                        echo  '会员' . $admin_name . '上传商品时，成本价不能大于售价！';
                        Db::rollback();
                    }

                    $value['attribute'] = serialize($attr_list_arr);
                    $value = self::array_key_remove($value, 'attr_list');
                    $attributes[$key]['costprice'] = $value['cbj'];
                    $attributes[$key]['yprice'] = $value['yj'];
                    $attributes[$key]['price'] = $value['sj'];
                    $attributes[$key]['num'] = $value['kucun'];
                    $attributes[$key]['total_num'] = $value['total_num'];
                    $attributes[$key]['min_inventory'] = $min_inventory;
                    $attributes[$key]['attribute'] = $value['attribute'];
                }
            }
            $maxSort = Db::name('lkt_product_list')->order('sort desc')->where(['store_id'=>$store_id,'recycle'=>0])->find();
            if(empty($maxSort)){
                $sort = 1;
            }else{
                $sort = $maxSort['sort']+1;
            }
            $s_type = $s_type ? $s_type : array();
            if (count($s_type) == 0)
            {
                $s_type = 0;
            }
            else
            {
                $s_type = implode(",", $s_type);
            }
            if($isVirtual != 1)
            {
                if (empty($active))
                {
                    echo  ' 请选择支持活动类型';
                    exit;
                }
                else
                {
                    if ($active == 1)
                    {
                        if ($show_adr)
                        {
                            if($type0 == '平台')
                            {
                                $show_adr = implode(",", $show_adr);
                            }
                            else if($type0 == 'PC店铺')
                            {
                                $show_adr = trim($show_adr);
                            }
                            else if($type0 == '店铺')
                            {
                                $show_adr = trim($show_adr,',');
                            }
                        }
                        $is_distribution = 0;
                    }
                    else if ($active == 5)
                    {
                        $show_adr = 0;
                        $is_distribution = 1;
                    }
                    else
                    {
                        $show_adr = 0;
                        $is_distribution = 0;
                    }
                }
            }
            else
            {
                $active = 1;
            }
            $virtual_volume = 0;
            //--处理属性
            if($virtual_volume != '' )
            {
                if (!is_numeric($virtual_volume) || strpos($virtual_volume, ".") !== false)
                {
                    echo ' 虚拟销量输入整数';
                    Db::rollback();
                }
                else
                {
                    if ($virtual_volume < 0)
                    {
                        echo  ' 虚拟销量请输入大于等于0的整数';
                        Db::rollback();
                    }
                }
            }
            else
            {
                $virtual_volume = 0;
            }
            $where = array(
                'store_id'=>$store_id,
                'id'=>$mch_id,
            );
            $r_mch = Db::name('lkt_mch')->where($where)->find();
            $shop_name = $r_mch['name'];
            $product_number = '';
            // 发布产品
            $prodData= array(
                'store_id'=>$store_id,
                'commodity_type'=>$isVirtual,
                'product_title'=>$data['title'],
                'subtitle'=> $url,
                'scan'=>$data['scan'],
                'product_number'=>$product_number,
                'product_class'=>$product_class,
                'brand_id'=>$brand_id,
                'label'=>'',
                'keyword'=>$data['title'],
                'weight'=>0,
                'imgurl'=>$cover,
                'sort'=>$sort,
                'content'=>$content,
                'richList'=>'',
                'min_inventory'=>$min_inventory,
                's_type'=>$s_type,
                'add_date'=>date('Y-m-d H:i:s'),
                'is_distribution'=>0,
                'distributor_id'=>0,
                'freight'=>'0',
                'active'=>$active,
                'mch_id'=>$mch_id,
                'mch_status'=>2,
                'show_adr'=>$show_adr,
                'initial'=>$initial,
                'publisher'=>$admin_name,
                'cover_map'=>trim($cover,'https://lottediskon.oss-accelerate.aliyuncs.com/'),
                'virtual_volume'=>$virtual_volume,
                'discount_rate'=>0,
            );
            Db::name('lkt_product_list')->data($prodData)->insert();
            $id1 = Db::name('lkt_product_list')->getLastInsID();
            if ($id1 >= 1)
            {
                if($type0 == '平台')
                {
                    if ($imgurls)
                    {
                        $arrimg = array();
                        $rf = Db::name('lkt_product_img')->where(['product_id'=>$id1])->find();
                        if ($rf)
                        {
                            foreach ($rf as $key => $fs)
                            {
                                $arrimg[$fs->product_url] = $fs->product_url;
                            }
                        }
                        foreach ($imgurls as $key => $file)
                        {
                            $imgsURL_name = preg_replace('/.*\//', '', $file);
                            if (array_key_exists($imgsURL_name, $arrimg))
                            {
                                unset($arrimg[$imgsURL_name]);
                            }
                            $lkt_product_img = array(
                                'product_url'=>$imgsURL_name,
                                'product_id'=>$id1,
                                'add_date'=>date('Y-m-d H:i:s'),
                            );
                            $r = Db::name('lkt_product_img')->data($lkt_product_img)->insert();
                            if ($r < 1)
                            {
                                echo ' 产品展示图上传失败！sql:';
                                exit;
                            }
                        }
                        if (!empty($arrimg))
                        {
                            foreach ($arrimg as $keys => $fss)
                            {
                                $r = Db::name('lkt_product_img')->where(['product_url'=>$fss])->delete();
                            }
                        }
                    }
                }
                else
                {
                    if (count($imgurls) != 0)
                    {
                        foreach ($imgurls as $k => $v)
                        {
                            $product_url = preg_replace('/.*\//', '', $v);
                            $lkt_product_img = array(
                                'product_url'=>$product_url,
                                'product_id'=>$id1,
                                'add_date'=>date('Y-m-d H:i:s'),
                            );
                            $r_x = Db::name('lkt_product_img')->data($lkt_product_img)->insert();
                            if ($r_x < 1)
                            {
                                echo  '店主' . $admin_name . '添加商品轮播图时，失败！sql:';
                                Db::rollback();
                            }
                        }
                    }
                }

                foreach ($attributes as $ke => $va)
                {
                    $va['pid'] = $id1;
                    $va['total_num'] = $va['num'];
                    $total_num = $va['num'];
                    $va['min_inventory'] = $min_inventory;
                    $va['ctime'] = date('Y-m-d H:i:s');
                    $va['color'] = empty($va['color'])?'':$va['color'];
                    $va['bar_code'] = empty($val['bar_code'])?'':$val['bar_code'];
                    $r_attribute = Db::name('lkt_configure')->data($va)->insert();
                    if ($r_attribute < 1)
                    {
                        $attributes1 = json_encode($va);
                        echo  $admin_name.' 属性数据添加失败！属性数据:'.$attributes1;
                        Db::rollback();
                    }
                    $num = $va['num'];
                    // 在库存记录表里，添加一条入库信息
                    $stockData = array(
                        'store_id'=>$store_id,
                        'product_id'=>$id1,
                        'attribute_id'=>$r_attribute,
                        'total_num'=>$total_num,
                        'flowing_num'=>$num,
                        'type'=>0,
                        'add_date'=>date('Y-m-d H:i:s'),
                        'content'=> $admin_name . '增加商品总库存' . $num,
                    );
                    $r_stock0 = Db::name('lkt_stock')->data($stockData)->insert();
                    if($r_stock0 < 1){
                        echo '库存记录添加失败';
                        Db::rollback();
                    }
                }
                $update = array(
                    'status'=>1
                );
                $where= array(
                    'store_id'=>$store_id,
                    'mch_id'=>$mch_id,
                    'operation'=>$admin_name,
                    'product_number'=>$product_number
                );
                $r_number = Db::name('lkt_product_number')->where($where)->update($update);
                if($r_number == -1){
                    echo '修改商品编码状态失败';
                    exit;
                }
            }
            Db::commit();
        }catch (Exception $e){
            Db::rollback();
            echo '保存失败，出错了'.$e->getMessage();
        }
        echo '保存成功'.$data['title'];
    }


    /**
     * [array_key_remove description]
     * <p>Copyright (c) 2018-2019</p>
     * <p>Company: www.laiketui.com</p>
     * @Author  苏涛
     * @param   [type]                   $arr [description]
     * @param   [type]                   $key [description]
     * @return  [type]          删除指定数组元素[description]
     * @version 2.0
     * @date    2019-01-08T17:44:00+0800
     */
    public static function array_key_remove($arr, $key)
    {
        if (!array_key_exists($key, $arr))
        {
            return $arr;
        }
        $keys = array_keys($arr);
        $index = array_search($key, $keys);
        if ($index !== false)
        {
            array_splice($arr, $index, 1);
        }
        return $arr;
    }
   public function trimall($str)//删除空格
    {
        /*$oldchar=array(" ","　","\t","\n","\r");
        $newchar=array("","","","","");
        $str = str_replace($oldchar,$newchar,$str);*/
       $str =  str_replace('/','',$str);
       $str = str_replace(',','',$str);
        $str = str_replace('.','',$str);
        $str = str_replace('+','',$str);
        $str = str_replace('"','',$str);
       return $str;
    }

    /**采集页面备份
     * @param $url
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    protected function back( $url){
        $client = new \GuzzleHttp\Client();
        $res = $client->request('GET', $url, [
            'headers' => [
                'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
                'Accept-Encoding' => 'gzip, deflate, br',
            ]
        ]);
        $html = (string)$res->getBody();
        $ql = QueryList::html($html);
        $data['title'] = $ql->find('h1.pdp-mod-product-badge-title')->text();
        $data['price'] = $ql->find('div.pdp-product-price .pdp-price_type_normal')->text();
        $data['priceDiscount'] = $ql->find('div.pdp-product-price .pdp-price_type_deleted')->text();
        $data['discount'] = $ql->find('div.pdp-product-price .pdp-product-price__discount')->text();
        $data['imglist']  = $ql->find('div .item-gallery__image-wrapper img')->attrs('src')->all();
        if(!empty( $data['imglist'])){
            foreach ( $data['imglist'] as &$val){
                $val = str_replace('_120x120q80.jpg_.webp','',$val);
            }
        }
        # $table = QueryList::html($html)->find('div.pdp-mod-product-info-section sku-prop-selection');
        $data['sku'] =  $ql->find('div .sku-selector img')->attrs('src')->all();
        $data['content'] = $ql->find('#block-6YHU-KbRDHh')->html();

    }

    /**通过列表获取全部url
     * @param $url
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function getAllUrl($url){
        $client = new \GuzzleHttp\Client();
        $res = $client->request('GET', $url, [
            'query' => ['wd' => 'QueryList'],
            'headers' => [
                'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
                'Accept-Encoding' => 'gzip, deflate, br',
            ]
        ]);
        $html = (string)$res->getBody();
        preg_match_all ('/(?:"productUrl"):"(.*?)"/', $html, $pat_array);
        return $pat_array;
    }

    /* $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
       $sheet = $spreadsheet->getActiveSheet();
       $sheet->setCellValueByColumnAndRow(1, 1, '商品编码(同一商品ID相同)');
       $sheet->setCellValueByColumnAndRow(2, 1, '商品标题');
       $sheet->setCellValueByColumnAndRow(3, 1, '副标题');
       $sheet->setCellValueByColumnAndRow(4, 1, '关键词');
       $sheet->setCellValueByColumnAndRow(5, 1, '属性(属性名和属性值用冒号隔开，不同属性名与属性名用分号隔开)');
       $sheet->setCellValueByColumnAndRow(6, 1, '成本价');
       $sheet->setCellValueByColumnAndRow(7, 1, '原价');
       $sheet->setCellValueByColumnAndRow(8, 1, '售价');
       $sheet->setCellValueByColumnAndRow(9, 1, '库存');
       $sheet->setCellValueByColumnAndRow(10,1, '单位');
       $sheet->setCellValueByColumnAndRow(11,1, '销量');
       $sheet->setCellValueByColumnAndRow(12,1, '条形码');
       $sheet->setCellValueByColumnAndRow(13,1, '运费ID');
       $sheet->setCellValueByColumnAndRow(14,1, '库存预警值');
       $sheet->setCellValueByColumnAndRow(15,1, '详细内容');
       $sheet->setCellValueByColumnAndRow(16,1, '主图（必填）');
       $sheet->setCellValueByColumnAndRow(17,1, '封面图（必填）');
       $sheet->setCellValueByColumnAndRow(18,1, '属性图（必填）');
       $sheet->setCellValueByColumnAndRow(19,1, '轮播图（必填）');
       #遍历输出
       $i=2;
       $id = time();
       foreach ($skuArr as $sku){
           $sheet->setCellValueByColumnAndRow(1, $i,$id);
           $sheet->setCellValueByColumnAndRow(2, $i, $data['title']);
           $sheet->setCellValueByColumnAndRow(3, $i, $data['title']);
           $sheet->setCellValueByColumnAndRow(4,$i, substr($data['title'],0,10));
           $sheet->setCellValueByColumnAndRow(5, $i, $sku['name']);
           $sheet->setCellValueByColumnAndRow(6, $i, $data['price']);
           $sheet->setCellValueByColumnAndRow(7, $i, $data['price']);
           $sheet->setCellValueByColumnAndRow(8, $i, $data['price']);
           $sheet->setCellValueByColumnAndRow(9, $i, '10000');
           $sheet->setCellValueByColumnAndRow(10,$i, '台');
           $sheet->setCellValueByColumnAndRow(11,$i, '');
           $sheet->setCellValueByColumnAndRow(12,$i, '');
           $sheet->setCellValueByColumnAndRow(13,$i, 1);
           $sheet->setCellValueByColumnAndRow(14,$i, '10');
           $sheet->setCellValueByColumnAndRow(15,$i, $conetent);
           $sheet->setCellValueByColumnAndRow(16,$i, $cover);
           $sheet->setCellValueByColumnAndRow(17,$i,$cover);
           $sheet->setCellValueByColumnAndRow(18,$i, $cover);
           $sheet->setCellValueByColumnAndRow(19,$i,  $data['imglist_str']);
           $i = $i+1;
       }
       header("Content-type:application/vnd.ms-excel;charset=UTF-8");
       header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
       header("Content-Disposition: attachment;filename='text.csv'");
       header('Cache-Control: max-age=0');
       // If you're serving to IE 9, then the following may be needed
       header('Cache-Control: max-age=1');
       // If you're serving to IE over SSL, then the following may be needed
       header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
       header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
       header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
       header('Pragma: public'); // HTTP/1.0
       $name = $this->trimall($data['title']);

       $writerCSV = new Csv($spreadsheet);;
       $writerCSV->setUseBom(false); // because of non-ASCII chars
       $writerCSV->save('download/'.$name.'.csv');*/
}